#version 330 core
out vec4 FragColor; //输出颜色
in vec3 Normal; //输入法向量
in vec3 FragPos; //顶点的世界空间坐标
in vec2 TexCoords; //输入纹理坐标

uniform vec3 viewPos; //观察者（摄像机）的位置


//定义材质机构体，移除了环境光材质颜色向量，因为环境光颜色在几乎所有情况下都等于漫反射颜色，所以我们不需要将它们分开储存
struct Material {

    sampler2D diffuse; //漫反射分量，纹理
    sampler2D specular; //镜面反射分量，纹理
    float shininess;
};
//定义光源结构体
struct Light {
    vec3 lightColor;
    vec3 direction; //平行光方向
    vec3 ambient; //环境光照
    vec3 diffuse; //漫反射分量
    vec3 specular; //镜面光分量
};

uniform Material material;
uniform Light light;
//镜面反射光照
void specularReflection (){
        // 环境光
       vec3 ambient = light.lightColor * light.ambient * vec3(texture(material.diffuse, TexCoords));

       // 漫反射
       vec3 norm = normalize(Normal);
       vec3 lightDir = normalize(-light.direction);// 取反，使得和法线方向的夹角为锐角
       float diff = max(dot(norm, lightDir), 0.0);
       vec3 diffuse = light.lightColor * light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));

       // 镜面光
       vec3 viewDir = normalize(viewPos - FragPos);
       vec3 reflectDir = reflect(-lightDir, norm);
       float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
       vec3 specular = light.lightColor * light.specular * spec * vec3(texture(material.specular, TexCoords));

       vec3 result = ambient + diffuse + specular;
       FragColor =  vec4(result, 1.0);
}

void main()
{

    specularReflection(); //镜面光照

}